module cl {
	export class AttackAction extends ActionPerformer {
		constructor(
				self_strength : number,
				self_morale : number,
				tgt_strength : number,
				tgt_morale : number) {
			super();
			this.type = 3;
			this.paramKeys = JSON.stringify(["self_strength","self_morale","tgt_strength","tgt_morale"]);
			this.paramVals = JSON.stringify([self_strength,self_morale,tgt_strength,tgt_morale]);
		}

		public accept() : void {

			let param = ActionUtil.getParamJSON(this);

			let troop = Campaign.troop[this.troopid];
			let targetTroopid = this.other_troopids[0];
			let targetTroop = Campaign.troop[targetTroopid];

			// 被攻击敌人看见自己
			ActionUtil.setAction(new SeeAction(0), targetTroopid, this.move_remain, troop.tid).accept();

			// 数据
			troop.move_remain = this.move_remain;
			targetTroop.move_remain = this.other_m_remain[0];

			troop.strength = param["self_strength"] as number;
			targetTroop.strength = param["tgt_strength"] as number;

			troop.morale = param["self_morale"] as number;
			targetTroop.morale = param["tgt_morale"] as number;

			troop.orientation = Map.tileEngine.headingIndex(Map.tileEngine.twoTidHeading(troop.tid, targetTroop.tid));
			targetTroop.orientation = Map.tileEngine.headingIndex(Map.tileEngine.twoTidHeading(targetTroop.tid, troop.tid));
		}

		public acceptOnce() : void { this.accept(); }

		public duration() : number {
			return 1500;
		}

		public play(map : CampaignPage) : void {

			let param = ActionUtil.getParamJSON(this);

			let troop = Campaign.troop[this.troopid];
			let targetTroopid = this.other_troopids[0];
			let targetTroop = Campaign.troop[targetTroopid];

			// 被攻击敌人看见自己
			ActionUtil.setAction(new SeeAction(0), targetTroopid, this.move_remain, troop.tid).play(map);

			// 动作动画
			let degree = Map.tileEngine.twoTidHeading(troop.tid, targetTroop.tid);
			let targetDegree = Map.tileEngine.twoTidHeading(targetTroop.tid, troop.tid);

			let troopGroup = map.groupTroop.getChildByName(String(this.troopid)) as eui.Group;
			let targetTroopGroup = map.groupTroop.getChildByName(String(targetTroopid)) as eui.Group;

			Util.setOrientation(troopGroup.getChildAt(1), Map.tileEngine.headingIndex(degree));
			Util.setOrientation(targetTroopGroup.getChildAt(1), Map.tileEngine.headingIndex(targetDegree));
			AttackEffect.attack(troopGroup.getChildAt(1), degree, 32, 10, 140);
			AttackEffect.shake(targetTroopGroup.getChildAt(1), 16, 350, 320);
			MoveRemainEffect.play(troopGroup, this.move_remain);
			MoveRemainEffect.play(targetTroopGroup, this.other_m_remain[0]);

			// 火力特效
			let blast = Util.setImageCoord("under_attack_png", Util.coordDegreeOffset(targetTroopGroup.x, targetTroopGroup.y, targetDegree, 25));
			let intervalCount = 0;
			let intervalHandle = setInterval(()=>{
				if (intervalCount == 0) {
					map.groupEffect.addChild(blast);

				} else if (intervalCount == 1) {
					map.groupEffect.removeChild(blast);

				} else if (intervalCount == 2) {
					clearInterval(intervalHandle);
					// 血条
					map.troopManager.drawBloodSymbol(troopGroup, param["self_strength"] as number, param["self_morale"] as number);
					map.troopManager.drawBloodSymbol(targetTroopGroup, param["tgt_strength"] as number, param["tgt_morale"] as number);
				}
				intervalCount++;
			}, 350);
		}
	}
}